package com.example.springpostbody.core.config;

import com.example.springpostbody.core.filter.CustomFilter;
import com.example.springpostbody.core.filter.PostRequestFilter;
import com.example.springpostbody.core.interceptor.PostRequestInterceptor;
import com.example.springpostbody.core.listener.AppStartupListener;
import com.example.springpostbody.core.servlet.RegisterBeanServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    private final PostRequestInterceptor postRequestInterceptor;

    //private final CustomFilter customFilter;

    public WebMvcConfig(PostRequestInterceptor postRequestInterceptor) {
        this.postRequestInterceptor = postRequestInterceptor;

    }

    // 可以定义多个bean
    @Bean
    public FilterRegistrationBean<CustomFilter> customFilter() {
        FilterRegistrationBean<CustomFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new CustomFilter());
        //registrationBean.setFilter(this.customFilter);
        registrationBean.addUrlPatterns("/*");  // 过滤特定路径
        //registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);  // 设置过滤器顺序
        registrationBean.setOrder(0);  // 设置过滤器顺序
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean<PostRequestFilter> postRequestFilter() {
        FilterRegistrationBean<PostRequestFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new PostRequestFilter());
        //registrationBean.setFilter(this.customFilter);
        registrationBean.addUrlPatterns("/*");  // 过滤特定路径
        registrationBean.setOrder(1);  // 设置过滤器顺序
        return registrationBean;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 可以配置多个
        registry.addInterceptor(postRequestInterceptor)
                .addPathPatterns("/**");  // 添加拦截路径
    }

    @Bean
    public ServletRegistrationBean<RegisterBeanServlet> registerBeanServlet() {
        ServletRegistrationBean<RegisterBeanServlet> registrationBean = new ServletRegistrationBean<>();
        registrationBean.addUrlMappings("/servlet/test");
        registrationBean.setServlet(new RegisterBeanServlet());
        return registrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean<AppStartupListener> myListener(){
        return new ServletListenerRegistrationBean<>(new AppStartupListener());
    }

    // 解决编码问题
    /*@Bean
    CharacterEncodingFilter characterEncodingFilter(){
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);
        return filter;
    }*/
}
